using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ChildrenOfWar
{
    //Holds level varibles, such as number of children, person objects, background texture, etc. Also controls functionality dependent on these varibles, such as the camera scroll
    //Note: After creating a level, be sure to intilize it useing initalize(game)
    class COWLevel
    {
        private COWGame Parent;//Holds pointer to parent so we can call it to get content assets, get the viewport for drawing, etc.

        private byte startY;
        private byte endY;
        private byte childNum;
        private string backgroundURL;

        //Varibles to intialized background and placement
        private Texture2D background;
        public COWCamera levelCam;//Camera for the level. Tied to level becuse it's location depends on the background.

        /// <summary>
        /// Creates new blank COWLevel.
        /// </summary>
        public COWLevel()
        {
            levelCam = new COWCamera();
        }

        /// <summary>
        /// Creates new a COWLevel with given varibles.
        /// </summary>
        /// <param name="startY">Vertical Y line that marks beginning of level</param>
        /// <param name="endY">Vertical Y line that marks goal of level</param>
        /// <param name="childNum">The number of children that need to be saved on the map</param>
        /// <param name="backgroundURL">The name of the asset for the background in the Content of the game</param>
        public COWLevel(byte startY, byte endY, byte childNum, String backgroundURL)
        {
            StartY = startY;
            EndY = endY;
            ChildNum = childNum;
            BackgroundURL = backgroundURL;
            levelCam = new COWCamera();
        }

        /// <summary>
        /// Called to initalize any neccisary level parameters, such as loading the background from the provided game's content. Returns a boolean representing weither the level was succesfully initalized or not.
        /// </summary>
        /// <param name="ParentGame">ParentGame that level is loading in. The level gets it's assets from its parent's content.</param>
        public Boolean Initalize(COWGame ParentGame)
        {
            Parent = ParentGame;
            if (backgroundURL == null)//return false if there isn't a background location since the load operation will fail.
            {
                Console.Error.WriteLine("Error, background was not set, could not generate background");
                return false;
            }
            background = ParentGame.Content.Load<Texture2D>(backgroundURL);
            MoveCamera(new Vector2(0, 0));//Set inital location at (0,0). This will get also cause the MoveCamera code to move the camera to a valid location if (0,0) is not a valid location.
            return true;
        }

        /// <summary>
        /// Called to tell the internal camera in level to move by the specified amount. Camera is programmed not to move beyond the edges of the background.
        /// </summary>
        /// <param name="moveVec">Vector specifiy how much to move the camera in the X and Y directions.</param>
        public void MoveCamera(Vector2 moveVec)
        {
            Viewport view = Parent.GraphicsDevice.Viewport;//shorthand for viewport

            //First move the camera, then do checks to see if we left the screen and mvoe it back if we went too far.
            levelCam.Move(moveVec);

            //X and Y are checked seperatly so that each can act independently. Check useing if-else since, if one is true the other can't possibly be so we mind as well skip it.
            if (levelCam.Pos.X - view.Width * 0.5 < 0)//too far left
            {
                levelCam.Pos = new Vector2(view.Width * 0.5f, levelCam.pos.Y);
            }
            else
            {
                if (levelCam.Pos.X + view.Width * 0.5 > background.Width)//too far right
                {
                    levelCam.Pos = new Vector2(background.Width - view.Width * 0.5f, levelCam.pos.Y);
                }
            }
            if (levelCam.Pos.Y - view.Height * 0.5 < 0)//too far up
            {
                levelCam.Pos = new Vector2(levelCam.pos.X, view.Height * 0.5f);
            }
            else
            {
                if (levelCam.Pos.Y + view.Height * 0.5 > background.Height)//too far down
                {
                    levelCam.Pos = new Vector2(levelCam.pos.X, background.Height - view.Height * 0.5f);
                }
            }
        }

        /// <summary>
        /// Called to draw the level. Uses parent's sprite batch.
        /// </summary>
        public void draw(SpriteBatch batch)
        {
            //batch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, levelCam.getCamTrans(Parent.graphics.GraphicsDevice));
            batch.Draw(background, new Vector2(0, 0), Color.White);
        }


        //geters and seters
        public byte StartY
        {
            get { return startY; }
            set { startY = value; }
        }

        public byte EndY
        {
            get { return endY; }
            set { endY = value; }
        }

        public byte ChildNum
        {
            get { return childNum; }
            set { childNum = value; }
        }

        public string BackgroundURL
        {
            get { return backgroundURL; }
            set { backgroundURL = value; }
        }

        //Background cannot be set directly
        public Texture2D Background
        {
            get { return background; }
        }

    }
}
